package cn.itcast.nems.common.web.controller.b;

import cn.itcast.ic.common.bean.BizError;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.BizException;
import cn.itcast.nems.base.dto.OriginChannelDTO;
import cn.itcast.nems.base.service.OriginChannelService;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.common.web.vo.DictionaryQueryVO;
import cn.itcast.nems.common.web.vo.OriginChannelEditVO;
import cn.itcast.nems.common.web.vo.OriginChannelVO;
import cn.itcast.nems.manager.icc.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单来源管理
 * </p>
 *
 * @author zhouwei
 * @since 2023/8/30 6:25 PM
 **/
@RestController
@RequestMapping("/b/origin_channel")
@Slf4j
public class OriginChannelController {
    @Autowired
    private OriginChannelService originChannelService;

    /**
     * 订单来源分页查询
     *
     * @param queryParam 请求参数
     * @return ResponseEntity<?>
     **/
    @PostMapping("/page/list")
    public ResponseEntity<?> pageList(@RequestBody BasePageRequest<DictionaryQueryVO<String>> queryParam)  throws Exception{
        queryParam.convert();
        Page<OriginChannelVO> pageData = Page.of(queryParam.getPageNumber(), queryParam.getPageSize(), 0, null);
        OriginChannelDTO originChannelDTO = new OriginChannelDTO();
        if (Objects.nonNull(queryParam.getCondition())) {
            if (!NumberUtil.checkNum(queryParam.getCondition().getId(),null)) {
                log.info("[订单来源分页查询] 请求参数 编号: {}, 包含非数字内容", queryParam.getCondition().getId());
                return ResponseEntity.ok().body(pageData );
            }
            BeanUtils.copyProperties(queryParam.getCondition(), originChannelDTO);
            originChannelDTO.setId(NumberUtil.format(queryParam.getCondition().getId(),10));
        }
        log.info("[订单来源分页查询] 请求参数: {} ", queryParam);
        PageQueryModel<OriginChannelDTO> queryModel = new PageQueryModel<>(queryParam.getPageNumber(),
                queryParam.getPageSize(), originChannelDTO);
        Page<OriginChannelDTO> data = originChannelService.findPageList(queryModel);
        List<OriginChannelVO> list = data.getRecords().stream().map(item -> {
                    OriginChannelVO originChannelVO = new OriginChannelVO();
                    BeanUtils.copyProperties(item, originChannelVO);
                    // originChannelVO.setId(item.getId());
                    return originChannelVO;
                }
        ).toList();
        pageData  = Page.of(data.getPageNumber(), data.getPageSize(), data.getRecordCount(), list);
        return ResponseEntity.ok().body(pageData);
    }

    /**
     * 新增业务线
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PostMapping()
    public ResponseEntity<?> addOriginChannel(@RequestBody OriginChannelEditVO editVO) throws Exception {
        if (Objects.isNull(editVO) || editVO.getName().isEmpty()) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
       if(editVO.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
           throw  new BizException(BizError.PARAMETER_ERROR);
       }
        log.info("[新增订单来源] 请求参数: {}", editVO);
        OriginChannelDTO originChannelDTO = new OriginChannelDTO(editVO.getId(), editVO.getCode(), null,
                editVO.getName(), editVO.getState());
        String result = originChannelService.addOriginChannel(originChannelDTO);
        return ResponseEntity.ok().body(result);
    }

    /**
     * 修改业务线
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PutMapping()
    public ResponseEntity<?> updateOriginChannel(@RequestBody OriginChannelEditVO editVO)  throws Exception{
        if (Objects.isNull(editVO) || editVO.getName().isEmpty() || Objects.isNull(editVO.getId())) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        if(editVO.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        log.info("[新增订单来源] 请求参数: {}", editVO);
        OriginChannelDTO originChannelDTO = new OriginChannelDTO(editVO.getId(), null, null,
                editVO.getName(), editVO.getState());
        String result = originChannelService.updateOriginChannel(originChannelDTO);
        return ResponseEntity.ok().body(result);
    }

    /**
     * 启用/禁用订单来源
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PutMapping("/state")
    public ResponseEntity<?> updateState(@RequestBody OriginChannelEditVO editVO) throws Exception{
        if (Objects.isNull(editVO) || Objects.isNull(editVO.getState()) || Objects.isNull(editVO.getId())) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        log.info("[新增订单来源] 请求参数: {}", editVO);
        OriginChannelDTO originChannelDTO = new OriginChannelDTO(editVO.getId(), null, null,
                editVO.getName(), editVO.getState());
        String  result = originChannelService.updateState(originChannelDTO);
        return ResponseEntity.ok().body(result);
    }
}
